from curl_cffi import requests
from lxml import etree
from util.ip import get_proxies
# 使用params字典替代URL中的参数
params = {
    "searchtype": "1",
    "page_index": "1",
    "bidSort": "0",
    "buyerName": "",
    "projectId": "",
    "pinMu": "0",
    "bidType": "0",
    "dbselect": "bidx",
    "kw": "郑州",
    "start_time": "2025:11:22",
    "end_time": "2025:11:22",
    "timeType": "6",
    "displayZone": "",
    "zoneId": "",
    "pppStatus": "0",
    "agentName": ""
}

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "Referer": "https://www.ccgp.gov.cn/"
}

try:
    res = requests.get(
        "https://search.ccgp.gov.cn/bxsearch",
        params=params,
        headers=headers,
        timeout=10,
        proxies=get_proxies()
    )

    print(f"状态码: {res.status_code}")
    print(f"响应长度: {len(res.text)}")
    print(f"最终请求URL: {res.url}")  # 查看自动构建的完整URL

    # 检查是否被反爬
    if "频繁访问" in res.text:
        print("❌ 触发反爬虫机制，IP被封禁")
        # 可以在这里添加处理逻辑
    else:
        html = etree.HTML(res.text)
        items = html.xpath('//ul[@class="vT-srch-result-list-bid"]/li')
        print(f"找到 {len(items)} 个招标项目")

        for item in items:
            # 使用相对路径
            title = item.xpath('.//a/text()')
            link = item.xpath('.//a/@href')
            if title:
                title_text = title[0].strip()
                link_url = link[0] if link else "无链接"
                print(f"标题: {title_text}")
                print(f"链接: {link_url}")
                print("-" * 50)

except Exception as e:
    print(f"请求错误: {e}")